package phidgetmotion.gui.util;

import javax.swing.AbstractListModel;

import phidgetmotion.classifier.Model;

public class ClassLabelModel extends AbstractListModel {
	private static final long serialVersionUID = -1118821099950994470L;
	private String[] classList = new String[0];
	
	public void reload(){
		Model model = Model.getModel();
		if(model!=null){
			String[] tmpClassList = model.getClasses();
			int diff = tmpClassList.length - classList.length;
			classList = tmpClassList;
			
			for(int i=0; i<classList.length; i++){
				classList[i] += " - " + model.getVectorsCount(i);
			}
			if(diff<0) fireIntervalRemoved(this, classList.length-diff, classList.length);
			else if(diff>0) fireIntervalAdded(this, classList.length-1, classList.length-diff);
			fireContentsChanged(this, 0, classList.length-1);
		}
	}
	
	public void update(int classId){
		Model model = Model.getModel();
		if(model!=null){
			classList[classId] = model.getClasses()[classId];
			classList[classId] += " - " + model.getVectorsCount(classId);	
			fireContentsChanged(this, classId, classId);
		}
	}
	
	@Override
	public Object getElementAt(int index) {
		if(index<0||index>=getSize()) return null;
		return classList[index];
	}

	@Override
	public int getSize() {
		return classList.length;
	}

}
